package model

import (
	"fmt"
	"project2/g"
	"time"
)

type SysDictionaryValue struct {

	// 主键ID
	Id uint `json:"id" gorm:"primaryKey;column:id"`

	// 名称
	Name string `json:"name" gorm:"column:name"`

	// 标识码
	Code string `json:"code" gorm:"column:code"`

	// 所属字典ID[关系=SysDictionary]
	DicId uint `json:"dicId" gorm:"column:dic_id"`

	// 状态
	State uint `json:"state" gorm:"column:state"`

	// 排序号
	OrderNum uint `json:"orderNum" gorm:"column:order_num"`

	// 备注
	Remarks string `json:"remarks" gorm:"column:remarks"`

	// 租户类型
	TenantType uint `json:"tenantType" gorm:"column:tenant_type"`

	// 租户ID
	TenantId uint `json:"tenantId" gorm:"column:tenant_id"`

	// 有效状态[数据字典=del_state]
	DelState uint `json:"delState" gorm:"column:del_state"`

	// 添加人
	AddUser string `json:"addUser" gorm:"column:add_user"`

	// 添加人ID
	AddUserId uint `json:"addUserId" gorm:"column:add_user_id"`

	// 添加时间
	AddTime time.Time `json:"addTime" gorm:"column:add_time"`

	// 最近更新人
	EditUser string `json:"editUser" gorm:"column:edit_user"`

	// 最近更新人ID
	EditUserId uint `json:"editUserId" gorm:"column:edit_user_id"`

	// 最近更新时间
	LastEditTime time.Time `json:"lastEditTime" gorm:"column:last_edit_time"`

	// 服务器编号ID
	ServerNumber uint `json:"serverNumber" gorm:"column:server_number"`

	//
	DelTime   time.Time  `json:"delTime" gorm:"column:del_time"`
	CreatedAt time.Time  `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt time.Time  `json:"updatedAt" gorm:"column:updated_at"`
	DeletedAt *time.Time `json:"deletedAt" gorm:"column:deleted_at"`
}

func (SysDictionaryValue) TableName() string {
	return "sys_dictionary_value"
}
func (self *SysDictionaryValue) UpdateEnity(id int, updatelog *string) {
	var oldData SysDictionaryValue
	g.Db.Model(&SysDictionaryValue{}).Where("id = ?", id).First(&oldData)
	if oldData.Id <= 0 {
		//todo error
		return
	}
	updateTx := g.Db.Model(&SysDictionaryValue{}).Where("id = ?", id)
	updateEnity := &SysDictionaryValue{}
	if self.Id != oldData.Id {
		updateEnity.Id = self.Id
		(*updatelog) += fmt.Sprintf("主键ID从 %s 修改为 %s", oldData.Id, self.Id)
	}
	if self.Name != oldData.Name {
		updateEnity.Name = self.Name
		(*updatelog) += fmt.Sprintf("名称从 %s 修改为 %s", oldData.Name, self.Name)
	}
	if self.Code != oldData.Code {
		updateEnity.Code = self.Code
		(*updatelog) += fmt.Sprintf("标识码从 %s 修改为 %s", oldData.Code, self.Code)
	}
	if self.DicId != oldData.DicId {
		updateEnity.DicId = self.DicId
		(*updatelog) += fmt.Sprintf("所属字典ID[关系=SysDictionary]从 %s 修改为 %s", oldData.DicId, self.DicId)
	}
	if self.State != oldData.State {
		updateEnity.State = self.State
		(*updatelog) += fmt.Sprintf("状态从 %s 修改为 %s", oldData.State, self.State)
	}
	if self.OrderNum != oldData.OrderNum {
		updateEnity.OrderNum = self.OrderNum
		(*updatelog) += fmt.Sprintf("排序号从 %s 修改为 %s", oldData.OrderNum, self.OrderNum)
	}
	if self.Remarks != oldData.Remarks {
		updateEnity.Remarks = self.Remarks
		(*updatelog) += fmt.Sprintf("备注从 %s 修改为 %s", oldData.Remarks, self.Remarks)
	}
	if self.TenantType != oldData.TenantType {
		updateEnity.TenantType = self.TenantType
		(*updatelog) += fmt.Sprintf("租户类型从 %s 修改为 %s", oldData.TenantType, self.TenantType)
	}
	if self.TenantId != oldData.TenantId {
		updateEnity.TenantId = self.TenantId
		(*updatelog) += fmt.Sprintf("租户ID从 %s 修改为 %s", oldData.TenantId, self.TenantId)
	}
	if self.DelState != oldData.DelState {
		updateEnity.DelState = self.DelState
		(*updatelog) += fmt.Sprintf("有效状态[数据字典=del_state]从 %s 修改为 %s", oldData.DelState, self.DelState)
	}
	if self.AddUser != oldData.AddUser {
		updateEnity.AddUser = self.AddUser
		(*updatelog) += fmt.Sprintf("添加人从 %s 修改为 %s", oldData.AddUser, self.AddUser)
	}
	if self.AddUserId != oldData.AddUserId {
		updateEnity.AddUserId = self.AddUserId
		(*updatelog) += fmt.Sprintf("添加人ID从 %s 修改为 %s", oldData.AddUserId, self.AddUserId)
	}
	if self.AddTime != oldData.AddTime {
		updateEnity.AddTime = self.AddTime
		(*updatelog) += fmt.Sprintf("添加时间从 %s 修改为 %s", oldData.AddTime, self.AddTime)
	}
	if self.EditUser != oldData.EditUser {
		updateEnity.EditUser = self.EditUser
		(*updatelog) += fmt.Sprintf("最近更新人从 %s 修改为 %s", oldData.EditUser, self.EditUser)
	}
	if self.EditUserId != oldData.EditUserId {
		updateEnity.EditUserId = self.EditUserId
		(*updatelog) += fmt.Sprintf("最近更新人ID从 %s 修改为 %s", oldData.EditUserId, self.EditUserId)
	}
	if self.LastEditTime != oldData.LastEditTime {
		updateEnity.LastEditTime = self.LastEditTime
		(*updatelog) += fmt.Sprintf("最近更新时间从 %s 修改为 %s", oldData.LastEditTime, self.LastEditTime)
	}
	if self.ServerNumber != oldData.ServerNumber {
		updateEnity.ServerNumber = self.ServerNumber
		(*updatelog) += fmt.Sprintf("服务器编号ID从 %s 修改为 %s", oldData.ServerNumber, self.ServerNumber)
	}
	if self.DelTime != oldData.DelTime {
		updateEnity.DelTime = self.DelTime
		(*updatelog) += fmt.Sprintf("从 %s 修改为 %s", oldData.DelTime, self.DelTime)
	}
	updateTx.Updates(updateEnity)
}
func (self *SysDictionaryValue) BeforeCreate() (err error) {
	self.CreatedAt = time.Now()
	self.AddTime = time.Now()
	return

}
func (self *SysDictionaryValue) BeforeUpdate() (err error) {
	self.LastEditTime = time.Now()
	return
}
